package com.wswy.wzcx.ui.main.home

import android.content.Context
import android.util.Log
import com.che.libcommon.utils.optional.Optional
import com.qq.e.ads.nativ.ADSize
import com.qq.e.ads.nativ.NativeExpressAD
import com.qq.e.ads.nativ.NativeExpressADView
import com.qq.e.comm.util.AdError
import com.wswy.wzcx.api.Api
import com.wswy.wzcx.api.Configs
import com.wswy.wzcx.model.AdModel
import com.wswy.wzcx.module.ad.AdConfigLoader
import com.wswy.wzcx.utils.RxUtils
import io.reactivex.Single
import io.reactivex.functions.BiFunction
import java.util.concurrent.atomic.AtomicReference
import java.util.concurrent.locks.AbstractQueuedSynchronizer
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.ReentrantLock

class HomeNewsLoader(private val context: Context) {

    private lateinit  var mADManager: NativeExpressAD


    private val loadADRef = AtomicReference<List<NativeExpressADView>?>()
    private val loadLock = ReentrantLock()
    private val loadCondition = loadLock.newCondition()


    private var seqId = 0L


    fun load(){
    }

    private fun loadGTD():Single<Optional<List<NativeExpressADView>?>>{

        loadADRef.set(null)

        if (!this::mADManager.isInitialized){
            mADManager = NativeExpressAD(context,
                    ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT),
                    AdConfigLoader.gdtConfig.appId,
                    AdConfigLoader.gdtConfig.feedId,
                    object : NativeExpressAD.NativeExpressADListener{
                        override fun onADCloseOverlay(adView: NativeExpressADView?) {
                        }

                        override fun onADLoaded(adList: MutableList<NativeExpressADView>?) {

                            loadADRef.set(adList)

                            try{
                                loadLock.tryLock()
                                loadCondition.signalAll()
                            }finally {
                                loadLock.unlock()
                            }
                        }

                        override fun onADOpenOverlay(adView: NativeExpressADView?) {

                        }

                        override fun onRenderFail(adView: NativeExpressADView?) {
                        }

                        override fun onADExposure(adView: NativeExpressADView?) {
                        }

                        override fun onADClosed(adView: NativeExpressADView?) {
                        }

                        override fun onADLeftApplication(adView: NativeExpressADView?) {
                        }

                        override fun onNoAD(p0: AdError?) {

                            try{
                                loadLock.tryLock()
                                loadCondition.signalAll()
                            }finally {
                                loadLock.unlock()
                            }
                        }

                        override fun onADClicked(adView: NativeExpressADView?) {
                        }

                        override fun onRenderSuccess(adView: NativeExpressADView?) {
                        }

                    })
        }

       return Single.create<Optional<List<NativeExpressADView>?>> {
            mADManager.loadAD(10)

            try{
                loadLock.tryLock()
                loadCondition.await()
            }finally {
                loadLock.unlock()
            }
            it.onSuccess(Optional.ofNullable(loadADRef.get()))
        }.compose(RxUtils.switch2io())


    }



}